/*************************************************************************
 * File Name:    Pow_x_n.cc
 * Author:       zero91
 * Mail:         jianzhang9102@gmail.com
 * Created Time: 2013/11/4 0:18:43
 * 
 * Description:  
 |------------------------------------------------------------------------
 | Problem: Pow(x, n)
 | Implement pow(x, n).
 |------------------------------------------------------------------------
 ************************************************************************/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <deque>
#include <map>
#include <set>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>

using namespace std;

class Solution {
public:
    double pow(double x, int n)
    {
        if (n == 0) return 1;
        
        double ans, t;
        long long p = n;
        
        if (p < 0) p = -p;
        
        ans = x, t = 1.0;
        while (p > 1) {
            if (p & 1) t *= ans;
            ans *= ans;
            p >>= 1;
        }
        if (n < 0) return 1.0 / (ans * t);
        return ans * t;
    }
};

